package com.example.qqlaobing.Umoney.view.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.qqlaobing.Umoney.R;
import com.example.qqlaobing.Umoney.adapter.LoanAdapter;
import com.example.qqlaobing.Umoney.adapter.LoanAdapter1;
import com.example.qqlaobing.Umoney.been.PostBean;
import com.example.qqlaobing.Umoney.net.NetRequestBusinessDefine;
import com.example.qqlaobing.Umoney.net.NetRequestRULBuilder;
import com.example.qqlaobing.Umoney.presenter.presenterImpl.OderListPresenterImpl;

import com.example.qqlaobing.Umoney.adapter.MyLoanAdapter;
import com.example.qqlaobing.Umoney.been.MyLoanBeen;
import com.example.qqlaobing.Umoney.view.activity.MainActivity;
import com.example.qqlaobing.Umoney.view.activity.MyWebView;
import com.example.qqlaobing.Umoney.view.fragment.bean.MyLoanBean;
import com.example.qqlaobing.Umoney.view.fragment.home.act.Act_LoanRecognition;
import com.example.qqlaobing.Umoney.view.myview.LoadingDialog;
import com.example.qqlaobing.Umoney.view.myview.RefreshListView;
import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * 我的贷款
 */

public class MyLoanFragment extends Fragment {
    private View rootView;
    private RecyclerView lvMyLoan;
    private List<MyLoanBeen> data;
    private SmartRefreshLayout mRefreshLayout;
    private int page = 1;
    LoanAdapter1 adapter1;
    List<MyLoanBean.DataBean.LoanListBean> datas = new ArrayList<>();
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (null == msg) {
                return;
            }
            switch (msg.what) {
                case NetRequestBusinessDefine.K_GET_LISTS:
                    if (msg.arg1 == 404) {
                        return;
                    }
                    JSONObject response = (JSONObject) msg.obj;
                    Gson gson = new Gson();
                    MyLoanBean bean = gson.fromJson(response.toString(), MyLoanBean.class);
                    if (bean.getStatus() == 1) {
                        for (int i = 0; i < bean.getData().getLoan_list().size(); i++) {
                            datas.add(bean.getData().getLoan_list().get(i));
                        }
                        if (adapter1 == null) {
                            adapter1 = new LoanAdapter1(datas);
                            adapter1.setOnItemChildClickListener((adapter, view, position) -> {
                                switch (view.getId()){
                                    case R.id.tv_type:
                                        String Pay_url = datas.get(position).getPay_url();
                                        Intent intent = new Intent();
                                        intent.setClass(getContext(), MyWebView.class);
                                        intent.putExtra("url", NetRequestBusinessDefine.K_HOST_NAME + Pay_url);
                                        startActivity(intent);
                                        break;
                                }
                            });
                            lvMyLoan.setAdapter(adapter1);
                        } else {
                            adapter1.notifyDataSetChanged();
                        }
                    } else {
                    }
                    break;
                default:
                    break;
            }
        }
    };
    private LoanAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_myloan, container, false);
        initView();
        initData();
        return rootView;
    }

    private void initData() {
        mRefreshLayout.setEnableRefresh(true);//是否启用下拉刷新功能
        mRefreshLayout.setEnableLoadMore(true);//是否启用上拉加载功能
        //内容跟随偏移
        mRefreshLayout.setEnableHeaderTranslationContent(true);
        mRefreshLayout.setOnRefreshListener(refreshlayout -> {
            datas.clear();
            page = 1;
            OderListPresenterImpl oderListPresenter = new OderListPresenterImpl(mHandler, "1", "10");
            oderListPresenter.postData();
            refreshlayout.finishRefresh(2000);
        });
        mRefreshLayout.setOnLoadMoreListener(refreshlayout -> {
            page++;
            OderListPresenterImpl oderListPresenter = new OderListPresenterImpl(mHandler, String.valueOf(page), "10");
            oderListPresenter.postData();
            refreshlayout.finishLoadMore(2000);
        });
        upData();
    }

    private void initView() {
        lvMyLoan = rootView.findViewById(R.id.lv_myloan);
        ImmersionBar.with(this).statusBarDarkFont(false).init();
        lvMyLoan.setLayoutManager(new LinearLayoutManager(getContext()));
        lvMyLoan.setNestedScrollingEnabled(false);
        mRefreshLayout = rootView.findViewById(R.id.refreshLayout);
    }

    public void upData() {
        OderListPresenterImpl oderListPresenter = new OderListPresenterImpl(mHandler, "1", "10");
        oderListPresenter.postData();
    }

}
